//
//  Tower.m
//  FrameWork
//
//  Created by Eagle on 5/31/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Tower.h"
#import "Parameter.h"
#import "MathTools.h"


@implementation Tower

@synthesize money = _money;
@synthesize sellMoney = _sellMoney;
@synthesize levelMoney = _levelMoney;

@synthesize level = _level;
@synthesize type = _type;
@synthesize interval = _interval;
@synthesize frame = _frame;
@synthesize range = _range;
@synthesize target = _target;

@synthesize myDegree = _myDegree;
@synthesize myArea = _myArea;
@synthesize myLastArea = _myLastArea;

@synthesize attackDegree = _attackDegree;
@synthesize turnSpeed = _turnSpeed;

@synthesize towerState = _towerState;
@synthesize attackAniLastTime = _attackAniLastTime;
@synthesize attackAniFrame = _attackAniFrame;

@synthesize clockOrWise = _clockOrWise;

+(id) tower:(int)type level:(int)level
{
	Tower *tower = nil;
	switch (type) {
		case 1:
			if((tower = [[[super alloc] initWithFile:@"jianta1A.png" rect:CGRectMake(0, 0, 84, 84)] autorelease]))
			{
				tower.type = type;
				tower.level = level;
				tower.money = [[Parameter sharedParameter] priceArrow:level];
				tower.sellMoney = [[Parameter sharedParameter] sellPriceArrow:level];
				tower.interval = [[Parameter sharedParameter] intervalArrow:level];
				tower.range = [[Parameter sharedParameter] radiusArrow:level];
				tower.attackDegree = [[Parameter sharedParameter] attackDegreeArrow:level];
				tower.turnSpeed = [[Parameter sharedParameter] turnSpeedArrow:level];
				if (level < 3) 
				{
					tower.levelMoney = [[Parameter sharedParameter] priceArrow:(level + 1)];
				}
				else {
					tower.levelMoney = 0;
				}

				tower.frame = tower.interval;
				tower.target = nil;
				tower.myDegree = 270.f;
				tower.myArea = GetAreaFromDegree(tower.myDegree);
				tower.attackAniLastTime = 5;
				tower.attackAniFrame = 0;
				tower.towerState = TOWERSTATE_SEARCH;
				//tower.offsetPosition = ccp(0, 50);
				//tower.offsetPosition = ccp(0, 7);
				//[tower changeOffPoint:ccp(0,50)];
				CCSprite *di = [CCSprite spriteWithFile:@"jiantaDI.png"];
				di.position = ccp(42, 42);
				//di.offsetPosition = ccp(0, 7);
				//[di changeOffPoint:ccp(0,7)];
				[di setTextureRect:CGRectMake(0, 0, 84, 84)];
				[tower addChild:di z:-1];
				
				
				
			}
			break;
		case 2:
			if((tower = [[[super alloc] initWithFile:@"duci1A.png" rect:CGRectMake(0, 0, 84, 84)] autorelease]))
			{
				tower.type = type;
				tower.level = level;
				tower.money = [[Parameter sharedParameter] pricePoison:level];
				tower.sellMoney = [[Parameter sharedParameter] sellPricePoison:level];
				tower.interval = [[Parameter sharedParameter] intervalPoison:level];
				tower.range = [[Parameter sharedParameter] radiusPoison:level];
				tower.attackDegree = [[Parameter sharedParameter] attackDegreePoison:level];
				tower.turnSpeed = [[Parameter sharedParameter] turnSpeedPoison:level];
				if (level < 3) 
				{
					tower.levelMoney = [[Parameter sharedParameter] pricePoison:(level + 1)];
				}
				else {
					tower.levelMoney = 0;
				}
				tower.frame = tower.interval;
				tower.target = nil;
				tower.myDegree = 270.f;
				tower.myArea = GetAreaFromDegree(tower.myDegree);
				tower.attackAniLastTime = 5;
				tower.attackAniFrame = 0;
				tower.towerState = TOWERSTATE_SEARCH;
				//tower.offsetPosition = ccp(0, 6);
				//[tower changeOffPoint:ccp(0,6)];
				
				
			}
			break;
		case 3:
			if((tower = [[[super alloc] initWithFile:@"chuanci1A.png" rect:CGRectMake(0, 0, 84, 84)] autorelease]))
			{
				tower.type = type;
				tower.level = level;
				tower.money = [[Parameter sharedParameter] pricePuncture:level];
				tower.sellMoney = [[Parameter sharedParameter] sellPricePuncture:level];
				tower.interval = [[Parameter sharedParameter] intervalPuncture:level];
				tower.range = [[Parameter sharedParameter] radiusPuncture:level];
				tower.attackDegree = [[Parameter sharedParameter] attackDegreePuncture:level];
				tower.turnSpeed = [[Parameter sharedParameter] turnSpeedPuncture:level];
				if (level < 3) 
				{
					tower.levelMoney = [[Parameter sharedParameter] pricePuncture:(level + 1)];
				}
				else {
					tower.levelMoney = 0;
				}
				tower.frame = tower.interval;
				tower.target = nil;
				tower.myDegree = 270.f;
				tower.myArea = GetAreaFromDegree(tower.myDegree);
				tower.attackAniLastTime = 5;
				tower.attackAniFrame = 0;
				tower.towerState = TOWERSTATE_SEARCH;
				//tower.offsetPosition = ccp(0, 10);
				//[tower changeOffPoint:ccp(0,10)];
				CCSprite *di = [CCSprite spriteWithFile:@"chuanciDI.png"];
				//di.offsetPosition = ccp(0, 10);
			//	[di changeOffPoint:ccp(0,50)];
				[di setTextureRect:CGRectMake(0, 0, 102, 102)];
				di.position = ccp(51, 51);
				[tower addChild:di z:-1];
				
			}
			break;
		case 4:
			if((tower = [[[super alloc] initWithFile:@"toushi1A.png" rect:CGRectMake(0, 0, 84, 84)] autorelease]))
			{
				tower.type = type;
				tower.level = level;
				tower.money = [[Parameter sharedParameter] priceMangonel:level];
				tower.sellMoney = [[Parameter sharedParameter] sellPriceMangonel:level];
				tower.interval = [[Parameter sharedParameter] intervalMangonel:level];
				tower.range = [[Parameter sharedParameter] radiusMangonel:level];
				tower.attackDegree = [[Parameter sharedParameter] attackDegreeMangonel:level];
				tower.turnSpeed = [[Parameter sharedParameter] turnSpeedMangonel:level];
				if (level < 3) 
				{
					tower.levelMoney = [[Parameter sharedParameter] priceMangonel:(level + 1)];
				}
				else {
					tower.levelMoney = 0;
				}
				tower.frame = tower.interval;
				tower.target = nil;
				tower.myDegree = 270.f;
				tower.myArea = GetAreaFromDegree(tower.myDegree);
				tower.attackAniLastTime = 5;
				tower.attackAniFrame = 0;
				tower.towerState = TOWERSTATE_SEARCH;
				//tower.offsetPosition = ccp(0, 13);
			//	[tower changeOffPoint:ccp(0,13)];
				CCSprite *di = [CCSprite spriteWithFile:@"toushiDI.png"];
			//	di.offsetPosition = ccp(0, 13);
			//	[di changeOffPoint:ccp(0,13)];
				[di setTextureRect:CGRectMake(0, 0, 74, 96)];
				di.position = ccp(37, 48);
				[tower addChild:di z:-1];
				
				
			}
			break;
		case 5:
			if((tower = [[[super alloc] initWithFile:@"zhangu.png"] autorelease]))
			{
				tower.type = type;
				tower.level = level;
				tower.money = [[Parameter sharedParameter] priceZhangu:level];
				tower.sellMoney = [[Parameter sharedParameter] sellPriceZhangu:level];
				tower.range = [[Parameter sharedParameter] radiusZhangu:level];
				if (level < 3) 
				{
					tower.levelMoney = [[Parameter sharedParameter] priceZhangu:(level + 1)];
				}
				else {
					tower.levelMoney = 0;
				}
				tower.frame = tower.interval;
				tower.target = nil;
			}
			break;
		default:
			break;
	}
	
	
	[tower InitRect];
	if (5 != tower.type) 
	{
		[tower SetRectFromArea:tower.myArea];
	}
	
	
	return tower;
}

-(void)InitRect
{
	switch (_type) 
	{
		case 1:
		{
			_myRect[0] = CGRectMake(168, 168, 84, 84);
			_myRect[1] = CGRectMake(84, 168, 84, 84);
			_myRect[2] = CGRectMake(0, 168, 84, 84);
			_myRect[3] = CGRectMake(168, 84, 84, 84);
			_myRect[4] = CGRectMake(84, 84, 84, 84);
			_myRect[5] = CGRectMake(0, 84, 84, 84);
			_myRect[6] = CGRectMake(168, 0, 84, 84);
			_myRect[7] = CGRectMake(84, 0, 84, 84);
			_myRect[8] = CGRectMake(0, 0, 84, 84);
		}
			break;
		case 2:
		{
			_myRect[0] = CGRectMake(168, 168, 84, 84);
			_myRect[1] = CGRectMake(84, 168, 84, 84);
			_myRect[2] = CGRectMake(0, 168, 84, 84);
			_myRect[3] = CGRectMake(168, 84, 84, 84);
			_myRect[4] = CGRectMake(84, 84, 84, 84);
			_myRect[5] = CGRectMake(0, 84, 84, 84);
			_myRect[6] = CGRectMake(168, 0, 84, 84);
			_myRect[7] = CGRectMake(84, 0, 84, 84);
			_myRect[8] = CGRectMake(0, 0, 84, 84);
		}
			break;
		case 3:
		{
			_myRect[8] = CGRectMake(0, 0, 102, 102);
			_myRect[7] = CGRectMake(102, 0, 102, 102);
			_myRect[6] = CGRectMake(204, 0, 102, 102);
			_myRect[5] = CGRectMake(306, 0, 102, 102);
			_myRect[4] = CGRectMake(408, 0, 102, 102);
			_myRect[3] = CGRectMake(0, 102, 102, 102);
			_myRect[2] = CGRectMake(102, 102, 102, 102);
			_myRect[1] = CGRectMake(204, 102, 102, 102);
			_myRect[0] = CGRectMake(306, 102, 102, 102);
		}
			break;
		case 4:
		{
			_myRect[8] = CGRectMake(0, 0, 74, 96);
			_myRect[7] = CGRectMake(0, 0, 74, 96);
			_myRect[6] = CGRectMake(148, 0, 74, 96);
			_myRect[5] = CGRectMake(222, 0, 74, 96);
			_myRect[4] = CGRectMake(296, 0, 74, 96);
			_myRect[3] = CGRectMake(0, 96, 74, 96);
			_myRect[2] = CGRectMake(74, 96, 74, 96);
			_myRect[1] = CGRectMake(148, 96, 74, 96);
			_myRect[0] = CGRectMake(222, 96, 74, 96);
		}
			break;
			
		default:
			break;
	}
}


-(void)SetRectFromArea:(int)area
{
	[self setTextureRect:_myRect[area]];
}



-(void)levelUp
{
	if (_level < 3) 
	{
		_level += 1;
		
		NSString *str = [NSString stringWithFormat:@"%d", _level];
		CCLabel* label = (CCLabel*)[self getChildByTag:kLevelLabel];
		[label setString:str];
		
		switch (_type) {
			case 1:
			{
				switch (_level) 
				{
					case 2:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"jianta2A.png"]];
					}
						break;
					case 3:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"jianta3A.png"]];
					}
						break;
					default:
						break;
				}
				
				_money = [[Parameter sharedParameter] priceArrow:_level];
				_sellMoney = [[Parameter sharedParameter] sellPriceArrow:_level];
				_interval = [[Parameter sharedParameter] intervalArrow:_level];
				_range = [[Parameter sharedParameter] radiusArrow:_level];
				_attackDegree = [[Parameter sharedParameter] attackDegreeArrow:_level];
				_turnSpeed = [[Parameter sharedParameter] turnSpeedArrow:_level];
				if (_level < 3) 
				{
					_levelMoney = [[Parameter sharedParameter] priceArrow:(_level + 1)];
				}
				else {
					_levelMoney = 0;
				}
			}
				break;
			case 2:
			{
				switch (_level) 
				{
					case 2:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"duci2A.png"]];
					}
						break;
					case 3:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"duci3A.png"]];
					}
						break;
					default:
						break;
				}
				
				_money = [[Parameter sharedParameter] pricePoison:_level];
				_sellMoney = [[Parameter sharedParameter] sellPricePoison:_level];
				_interval = [[Parameter sharedParameter] intervalPoison:_level];
				_range = [[Parameter sharedParameter] radiusPoison:_level];
				_attackDegree = [[Parameter sharedParameter] attackDegreePoison:_level];
				_turnSpeed = [[Parameter sharedParameter] turnSpeedPoison:_level];
				if (_level < 3) 
				{
					_levelMoney = [[Parameter sharedParameter] pricePoison:(_level + 1)];
				}
				else {
					_levelMoney = 0;
				}
			}
				break;
			case 3:
			{
				
				switch (_level) 
				{
					case 2:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"chuanci2A.png"]];
					}
						break;
					case 3:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"chuanci3A.png"]];
					}
						break;
					default:
						break;
				}
				_money = [[Parameter sharedParameter] pricePuncture:_level];
				_sellMoney = [[Parameter sharedParameter] sellPricePuncture:_level];
				_interval = [[Parameter sharedParameter] intervalPuncture:_level];
				_range = [[Parameter sharedParameter] radiusPuncture:_level];
				_attackDegree = [[Parameter sharedParameter] attackDegreePuncture:_level];
				_turnSpeed = [[Parameter sharedParameter] turnSpeedPuncture:_level];
				if (_level < 3) 
				{
					_levelMoney = [[Parameter sharedParameter] pricePuncture:(_level + 1)];
				}
				else {
					_levelMoney = 0;
				}
			}
				break;
			case 4:
			{
				switch (_level) 
				{
					case 2:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"toushi2A.png"]];
					}
						break;
					case 3:
					{
						[self setTexture:[[CCTextureCache sharedTextureCache] addImage:@"toushi3A.png"]];
					}
						break;
					default:
						break;
				}
				
				_money = [[Parameter sharedParameter] priceMangonel:_level];
				_sellMoney = [[Parameter sharedParameter] sellPriceMangonel:_level];
				_interval = [[Parameter sharedParameter] intervalMangonel:_level];
				_range = [[Parameter sharedParameter] radiusMangonel:_level];
				_attackDegree = [[Parameter sharedParameter] attackDegreeMangonel:_level];
				_turnSpeed = [[Parameter sharedParameter] turnSpeedMangonel:_level];
				if (_level < 3) 
				{
					_levelMoney = [[Parameter sharedParameter] priceMangonel:(_level + 1)];
				}
				else 
				{
					_levelMoney = 0;
				}
			}
				break;
			case 5:
			{
				_money = [[Parameter sharedParameter] priceZhangu:_level];
				_sellMoney = [[Parameter sharedParameter] sellPriceZhangu:_level];
				//_interval = [[Parameter sharedParameter] intervalArrow:_level];
				_range = [[Parameter sharedParameter] radiusZhangu:_level];
				if (_level < 3) 
				{
					_levelMoney = [[Parameter sharedParameter] priceZhangu:(_level + 1)];
				}
				else {
					_levelMoney = 0;
				}
			}
				break;
			default:
				break;
		}
		
	}
	
}

-(void)changeToAttack
{
	switch (_type) 
	{
		case 1:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"jianta1B.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"jianta2B.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"jianta3B.png"]];
				}
					break;
				default:
					break;
			}
			
		}
			break;
		case 2:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"duci1B.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"duci2B.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"duci3B.png"]];
				}
					break;
				default:
					break;
			}
		}
			break;
		case 3:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"chuanci1B.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"chuanci2B.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"chuanci3B.png"]];
				}
					break;
				default:
					break;
			}
		}
			break;
		case 4:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"toushi1B.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"toushi2B.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"toushi3B.png"]];
				}
					break;
				default:
					break;
			}
		}
			break;
		default:
			break;
	}
}

-(void)changeToNormal
{
	switch (_type) 
	{
		case 1:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"jianta1A.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"jianta2A.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"jianta3A.png"]];
				}
					break;
				default:
					break;
			}
			
		}
			break;
		case 2:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"duci1A.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"duci2A.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"duci3A.png"]];
				}
					break;
				default:
					break;
			}
		}
			break;
		case 3:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"chuanci1A.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"chuanci2A.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"chuanci3A.png"]];
				}
					break;
				default:
					break;
			}
		}
			break;
		case 4:
		{
			switch (_level) 
			{
				case 1:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"toushi1A.png"]];
				}
					break;
				case 2:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"toushi2A.png"]];
				}
					break;
				case 3:
				{
					[self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"toushi3A.png"]];
				}
					break;
				default:
					break;
			}
		}
			break;
		default:
			break;
	}
}


-(void)dealloc
{
	[super dealloc];
	if (_target) 
	{
		[_target release];
	}
}

@end




